package cn.edu.sdjzu.xg.bysj.service;
//201902104061徐中堂
import cn.edu.sdjzu.xg.bysj.dao.RoleDao;
import cn.edu.sdjzu.xg.bysj.domain.authority.Role;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Collection;
import java.util.List;

public final class RoleService {
    //新建RoleDao实例
	private static final RoleDao roleDao = RoleDao.getInstance();
	//新建RoleService对象
	private static final RoleService roleService = new RoleService();
	//获取RoleService对象
    public static RoleService getInstance() {
        return roleService;
    }
	/**
	 * 向RoleeDao发消息根据页码查找
	 * @param pagination
	 * @param conditionList
	 * @return
	 * @throws SQLException
	 */
	public Collection<Role> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Collection<Role> roles = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			roles = roleDao.findAll(connection, pagination, conditionList);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return roles;
	}
	/**
	 * 向RoleeDao发消息根据id查找
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Role find(Integer id) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Role role = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			role = roleDao.find(connection, id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return role;
	}
	/**
	 * 向RoleeDao发消息更新
	 * @param role
	 * @return
	 * @throws SQLException
	 */
	public boolean update(Role role) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean updated = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			updated = roleDao.update(connection, role);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return updated;
	}
	/**
	 * 向RoleeDao发消息新增
	 * @param role
	 * @return
	 * @throws SQLException
	 */
	public boolean add(Role role) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean added = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			added = roleDao.add(connection, role);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return added;
	}
	/**
	 * 向RoleeDao发消息根据id删除
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public boolean delete(Integer id) throws SQLException, BysjException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean deleted = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			deleted = roleDao.delete(connection, id);
			//提交事务
			connection.commit();

		} catch (SQLException e) {
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return deleted;
	}

}
